/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package recommender.dbi;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import recommender.objects.Parent;
import recommender.objects.Video;

/**
 *
 * @author Aya
 */
public class LTVI extends VBI{
    
    private final EntityManager em;
    
    public LTVI(){
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("RecommenderPU");
        em = factory.createEntityManager();   
    }
    
    @Override
    public Video getVideo(String identifier) throws Exception{
        Query q = em.createNativeQuery("SELECT * FROM vb.get_video(?1)", Video.class);
        q.setParameter(1, Integer.parseInt(identifier.replace("Video", "")));
        List<Video> v = q.getResultList();
        if(v != null && !v.isEmpty())
            return v.get(0);
        else
            throw new Exception("Entity not found");
    }

    @Override
    public List<Video> getAllVideos(int src) {
        Query q = em.createNativeQuery("SELECT * FROM vb.get_all_videos(?1)", Video.class);
        q.setParameter(1, src);
        return q.getResultList();
    }

    @Override
    public List<Parent> getAnnotations(int id) {
        Query q = em.createNativeQuery("SELECT * FROM vb.get_video_annotations(?1)", Parent.class);
        q.setParameter(1, id);
        return q.getResultList();
    }

    @Override
    public List<Parent> getAnnotations(String identifier) {
        return getAnnotations(Integer.parseInt(identifier.replace("Video", "")));
    }

    @Override
    public List<Video> getVideosWithTopic(String topic) {
        Query q = em.createNativeQuery("SELECT * FROM vb.get_videos_with_topic(?1)", Video.class);
        q.setParameter(1, topic);
        return q.getResultList();
    }

    @Override
    public List<Video> searchVideos(String keyword, int mode) {
        Query q = em.createNativeQuery("SELECT * FROM vb.search_videos(?1, ?2)", Video.class);
        q.setParameter(1, keyword);
        q.setParameter(2, mode);
        return q.getResultList();
    }
    
    public List<Video> getNVideos(int n) {
        Query q = em.createNativeQuery("SELECT * FROM vb.get_n_videos(?1)", Video.class);
        q.setParameter(1, n);
        return q.getResultList();
    }
    
    public List<Video> getVideosTill(int n) {
        Query q = em.createNativeQuery("SELECT * FROM vb.video WHERE id < ?1", Video.class);
        q.setParameter(1, n);
        return q.getResultList();
    }
    
    public List<Integer> getTags(int mode) {
        Query q = em.createNativeQuery("SELECT * FROM vb.get_all_tags(?1)");
        q.setParameter(1, mode);
        return q.getResultList();
    }
    
    public List<Integer> getVideosTagged(int tag, int mode) {
        Query q = em.createNativeQuery("SELECT * FROM vb.get_videos_tagged(?1, ?2)");
        q.setParameter(1, tag);
        q.setParameter(2, mode);
        return q.getResultList();
    }
    
    public List<Integer> getVideosRelated(int tag, int mode) {
        Query q = em.createNativeQuery("SELECT * FROM vb.get_related_videos(?1, ?2)");
        q.setParameter(1, tag);
        q.setParameter(2, mode);
        return q.getResultList();
    }
    
    public List<Integer> getTEDVideosRelated(int tag, int mode) {
        Query q = em.createNativeQuery("SELECT * FROM vb.get_ted_related(?1, ?2)");
        q.setParameter(1, tag);
        q.setParameter(2, mode);
        return q.getResultList();
    }
    
    public List<Integer> getALlTEDVideosRelated(int mode) {
        Query q = em.createNativeQuery("SELECT * FROM vb.get_all_related_videos(?1)");
        q.setParameter(1, mode);
        return q.getResultList();
    }
    
    public int getSetSize(int mode){
        Query q = em.createNativeQuery("SELECT * FROM vb.get_set_size(?1)");
        q.setParameter(1, mode);
        return (Integer) q.getSingleResult();
    }
    
    public double diversity(List<Integer> rec){
        String rec_list = convert(rec);
        Query q = em.createNativeQuery("SELECT * FROM vb.diversity("+rec_list+")");
        return (Double) q.getSingleResult();
    }
    
    private String convert(List<Integer> rec){
        String s = "'{";
        for(int i=0; i<rec.size(); i++){
            if(i==0)
                s += rec.get(i);
            else
                s += "," + rec.get(i);
        }
        return s + "}'";
    }
    
}
